var loc = (location.href.match(/maniaco=([0-9]+)/i));
loc = loc==null? "": loc[1];
if (location.href.match(/^http:\/\/(www\.)?putlocker\.com/i) && loc != "") {
	addScript("putlocker"+loc);
} else if (location.href.match(/^http:\/\/(www\.)?megaupload\.com/i) && loc != "") {
	addScript('mega'+loc);
} else if (window.location.href.match(/^http:\/\/(www\.)?wupload\.(com|cn|de|es|fr|co\.uk|com\.hk|in|it|jp|mx)/i) && loc != "") {
	addScript('wupload'+loc);
} else if (location.href.match(/^http:\/\/www\.pelimaniaco\.com/i) && loc == "") {
	getVersion(function(v){
		if (document.getElementById("reproductor2")) {
			if(document.getElementById("plug_v"+v)){
				var video = document.getElementById("reproductor2").src.split("?");
				document.getElementById("reproductor2").src = "http://www.pelimaniaco.com/player/video/?"+video[1];
			}
		}
	});
}

function addScript(id) { 
	var s = document.createElement('script');
	s.setAttribute("type","text/javascript");
	s.setAttribute("src", "http://www.pelimaniaco.com/plug/servers/"+id+".js");
	document.getElementsByTagName("head")[0].appendChild(s);
}

function getVersion(callback) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open('GET', chrome.extension.getURL('manifest.json'));
        xmlhttp.onload = function (e) {
            var manifest = JSON.parse(xmlhttp.responseText);
            callback(manifest.version);
        }
        xmlhttp.send(null);
}
